<template>
    <div>
        <div class="e-table" style="background-color: #fff" v-if="showTable">
            <!-- 头部buttons -->
            <div class="top">
                <div class="left">
                    <div v-if="isAdd">
                        <el-button size="small" class="btn-greenYellow" @click="importList"
                        >引入清单</el-button>
                    </div>
                </div>
                 <div class="right">
                    <div class="squarecolor">
                        <div class="color colorAdd"></div>
                        <span>新增</span>
                    </div>
                    <div class="squarecolor item">
                        <div class="color colorUpdate"></div>
                        <span>修改</span>
                    </div>
                    <div class="squarecolor">
                        <div class="color colorDel"></div>
                        <span>删除/过期</span>
                    </div>
                    <div style="margin-left:20px">
                        <el-input
                            placeholder="请输入细目编号、细目名称或结构部位进行搜索"
                            size="small"
                            v-model="searchKey"
                            @blur="search"
                        >
                            <i slot="suffix" class="el-input__icon el-icon-search" @click="search"></i>
                        </el-input>
                    </div>
                </div>
            </div>
            <div class="e-tree-table">
                <div class="errorMsg" v-if="errorMsg.length>0">
                    <span >
                        {{ errorMsg.join(',') }}
                    </span>
                </div>
                <el-table
                    v-show="!isSearch"
                    ref="tableRef"
                    border
                    style="width: 100%"
                    row-key="billId"
                    :data="tableData"
                    :tree-props="{
                        children: 'children',
                        hasChildren: 'hasChildren'
                    }"
                    :load="onload"
                    lazy
                    :row-class-name="tableRowClassName"
                    class="table"
                    :max-height="$store.state.tableHeight"
                    highlight-current-row
                    v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'tableRef' }"
                >
                    <el-table-column label="操作" width="200" align='left'>
                        <template slot-scope="{ row }">
                            <div class="oparation_box">
                                <!-- <img src="@/assets/btn/sub.png" class="add_sub"
                                    @click="addChilden(row)"
                                    v-if="(row.listIsDetailed || row.identification === '项') && !row.structureId && isAdd && row.changeState !== -1"
                                    title="添加子级"
                                    /> -->
                                <i class="el-icon-delete"
                                    style="font-size: 24px; color: #e9513e;"
                                    v-if="isAdd && row.changeState !== -1"
                                    @click="onDelNode(row)"
                                    title="删除"
                                ></i>
                                <img src="@/assets/btn/cancelDelete.png" class="cancel_delete"
                                    @click="cancelDelete(row)"
                                        v-if="isAdd && row.changeState === -1"
                                    title="取消删除"
                                />
                                <img src="@/assets/btn/addList.png" class="add_sub"
                                    @click="supplyCount(row)"
                                    v-if=" isTerm(row) && row.structureId && isAdd && isUnitprice(row)"
                                    title="计算甲供清单"
                                    />
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.itemNo"
                                placeholder="请输入内容"
                                v-if="row.identification === '项' && !row.structureId && row.isNew && row.changeState !== -1"
                            ></el-input>
                            <div v-else>{{ row.itemNo }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.itemName"
                                placeholder="请输入内容"
                                v-if="row.identification === '项' && !row.structureId && row.isNew && row.changeState !== -1"
                            ></el-input>
                            <div v-else>{{ row.itemName }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="structureNamePaths" label="结构部位" width="250">
                        <template slot-scope="{ row }">
                            <div>{{ row.structureNamePaths }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unit" align="center" label="计量单位" width="120">
                        <template slot-scope="{ row }">
                            <el-select
                                v-model="row.unit"
                                v-if="row.itemIsDetailed && row.identification === '项' && row.isNew && row.changeState !== -1"
                                @change="changeUnit(row)"
                            >
                                <el-option
                                    v-for="item in unitList"
                                    :value="item.label"
                                    :label="item.label"
                                    :key="item.label"
                                >
                                </el-option>
                            </el-select>
                            <div v-else>{{ row.unit }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="identification"  align="center"  label="标识" width="120">
                        <template slot-scope="{ row }">
                            <div v-if="!row.structureId">{{ row.identification }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceType"  align="center"  label="单价类型" width="150">
                        <template slot-scope="{ row }">
                            <el-select
                                v-model="row.unitPriceType"
                                placeholder="请选择"
                                    v-if="row.listIsDetailed && row.isNew && row.changeState !== -1 && !row.structureId"
                                    @change="unitPriceTypeChange(row)"
                            >
                                <el-option
                                    label="全单价"
                                    :value="1"
                                ></el-option>
                                <el-option
                                    label="劳务单价"
                                    :value="2"
                                ></el-option>
                            </el-select>
                                <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantity" label="原数量"  align="right"  width="120">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <span v-else>{{ row.quantity }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPrice" label="原单价" align="right" width="120">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPrice }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amount" label="原金额(元)" align="right" width="120">
                        <template slot-scope="{ row }">
                            <div>{{ row.amount }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityAfterChange" label="本次变更数量" class-name="historyData"  align="right"  width="120">
                        <template slot-scope="{ row }">
                             <el-input
                                v-model="row.quantityAfterChange"
                                placeholder="请输入内容"
                                v-if="row.structureId && showParentUnit(row) !=='总额' && isEdit && row.changeState !== -1"
                                v-enterFloat2
                                @change="quantityChange(row)"
                            ></el-input>
                            <span v-else-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <span v-else>{{ row.quantityAfterChange }}</span>
                            <historyUpdateTable
                                v-if="checkEmpty(row.numberRecords)"
                                @clickIcon="clickIcon(row.numberRecords)"
                                :historyTable="historyTableData"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceAfterChange" label="本次变更单价" class-name="historyData" align="right" width="120">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.unitPriceAfterChange"
                                placeholder="请输入内容"
                                v-if="row.unit !== '总额' && row.itemIsDetailed && !row.structureId && isEdit && row.changeState === 1"
                                v-enterFloat
                                @change="unitPriceChange(row)"
                            ></el-input>
                            <div v-else-if="row.unit === '总额'">--</div>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPriceAfterChange }}</div>
                            <historyUpdateTable
                                v-if="checkEmpty(row.priceRecords)"
                                @clickIcon="clickIcon(row.priceRecords)"
                                :historyTable="historyTableData"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountAfterChange" label="本次变更金额" class-name="historyData" align="right" width="120">
                        <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.amountAfterChange"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit && row.changeType !== -2"
                                    @input="changeAmount(row)"
                                    v-enterAllInteger1
                                ></el-input>
                                <div v-else>{{ row.amountAfterChange }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.amountRecords)"
                                    @clickIcon="clickIcon(row.amountRecords)"
                                    :historyTable="historyTableData"
                                />
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityLastChange" label="截止上期末变更后数量" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.quantityLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceLastChange" label="截止上期末变更后单价" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPriceLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountLastChange" label="截止上期末变更后金额" align="right" width="200">
                        <template slot-scope="{ row }">
                            <div>{{ row.amountLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityEndChange" label="截止本期末变更后数量" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ quantityEndChange(row) }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceEndChange" label="截止本期末变更后单价" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ unitPriceEndChange(row) }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountEndChange" label="截止本期末变更后金额" align="right" width="200">
                        <template slot-scope="{ row }">
                            <div>{{ row.amountEndChange }}</div>
                        </template>
                    </el-table-column>
                </el-table>

                <el-table
                    v-show="isSearch"
                    ref="tableRef"
                    border
                    style="width: 100%"
                    row-key="billId"
                    :data="tableData"
                    :tree-props="{
                        children: 'children',
                        hasChildren: 'hasChildren'
                    }"
                    :load="onload"
                    lazy
                    :row-class-name="tableRowClassName"
                    class="table"
                    :max-height="$store.state.tableHeight"
                    highlight-current-row
                    v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'tableRef' }"
                >
                    <el-table-column label="操作" width="200" align='left'>
                    </el-table-column>
                   <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.itemNo"
                                placeholder="请输入内容"
                                v-if="row.identification === '项' && !row.structureId && row.isNew && row.changeState !== -1"
                            ></el-input>
                            <div v-else>{{ row.itemNo }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.itemName"
                                placeholder="请输入内容"
                                v-if="row.identification === '项' && !row.structureId && row.isNew && row.changeState !== -1"
                            ></el-input>
                            <div v-else>{{ row.itemName }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="structureNamePaths" label="结构部位" width="250">
                        <template slot-scope="{ row }">
                            <div>{{ row.structureNamePaths }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unit" align="center" label="计量单位" width="120">
                        <template slot-scope="{ row }">
                            <el-select
                                v-model="row.unit"
                                v-if="row.itemIsDetailed && row.identification === '项' && row.isNew && row.changeState !== -1"
                                @change="changeUnit(row)"
                            >
                                <el-option
                                    v-for="item in unitList"
                                    :value="item.label"
                                    :label="item.label"
                                    :key="item.label"
                                >
                                </el-option>
                            </el-select>
                            <div v-else>{{ row.unit }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="identification"  align="center"  label="标识" width="120">
                        <template slot-scope="{ row }">
                            <div v-if="!row.structureId">{{ row.identification }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceType"  align="center"  label="单价类型" width="150">
                        <template slot-scope="{ row }">
                            <el-select
                                v-model="row.unitPriceType"
                                placeholder="请选择"
                                    v-if="row.listIsDetailed && row.isNew && row.changeState !== -1 && !row.structureId"
                                    @change="unitPriceTypeChange(row)"
                            >
                                <el-option
                                    label="全单价"
                                    :value="1"
                                ></el-option>
                                <el-option
                                    label="劳务单价"
                                    :value="2"
                                ></el-option>
                            </el-select>
                                <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantity" label="原数量"  align="right"  width="120">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <span v-else>{{ row.quantity }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPrice" label="原单价" align="right" width="120">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPrice }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amount" label="原金额(元)" align="right" width="120">
                        <template slot-scope="{ row }">
                            <div>{{ row.amount }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityAfterChange" label="本次变更数量" class-name="historyData"  align="right"  width="120">
                        <template slot-scope="{ row }">
                             <el-input
                                v-model="row.quantityAfterChange"
                                placeholder="请输入内容"
                                v-if="(row.structureId && showParentUnit(row) !=='总额') && isEdit && row.changeState !== -1"
                                v-enterFloat2
                                @change="quantityChange(row)"
                            ></el-input>
                            <span v-else-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <span v-else>{{ row.quantityAfterChange }}</span>
                            <historyUpdateTable
                                v-if="checkEmpty(row.numberRecords)"
                                @clickIcon="clickIcon(row.numberRecords)"
                                :historyTable="historyTableData"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceAfterChange" label="本次变更单价" class-name="historyData" align="right" width="120">
                        <template slot-scope="{ row }">
                            <el-input
                                v-model="row.unitPriceAfterChange"
                                placeholder="请输入内容"
                                v-if="row.unit !== '总额' && row.itemIsDetailed && !row.structureId && isEdit && row.changeState === 1"
                                v-enterFloat
                                @change="unitPriceChange(row)"
                            ></el-input>
                            <div v-else-if="row.unit === '总额'">--</div>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPriceAfterChange }}</div>
                            <historyUpdateTable
                                v-if="checkEmpty(row.priceRecords)"
                                @clickIcon="clickIcon(row.priceRecords)"
                                :historyTable="historyTableData"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountAfterChange" label="本次变更金额" class-name="historyData" align="right" width="120">
                        <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.amountAfterChange"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit && row.changeType !== -2"
                                    @input="changeAmount(row)"
                                    v-enterAllInteger1
                                ></el-input>
                                <div v-else>{{ row.amountAfterChange }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.amountRecords)"
                                    @clickIcon="clickIcon(row.amountRecords)"
                                    :historyTable="historyTableData"
                                />
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityLastChange" label="截止上期末变更后数量" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.quantityLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceLastChange" label="截止上期末变更后单价" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ row.unitPriceLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountLastChange" label="截止上期末变更后金额" align="right" width="200">
                        <template slot-scope="{ row }">
                            <div>{{ row.amountLastChange }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantityEndChange" label="截止本期末变更后数量" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ quantityEndChange(row) }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="unitPriceEndChange" label="截止本期末变更后单价" align="right" width="200">
                        <template slot-scope="{ row }">
                            <span v-if="row.unit === '总额'">--</span>
                            <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                            <div v-else>{{ unitPriceEndChange(row) }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amountEndChange" label="截止本期末变更后金额" align="right" width="200">
                        <template slot-scope="{ row }">
                            <div>{{ row.amountEndChange }}</div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>
<script>
// import tenderRequest from '@/api/tender/tender.js'
import { toLocation, vueDom } from '@/utils/common.js'
import { uidBuilder } from '@/api/base/uid.js'
import { getUnit } from '@/api/subcontractApi/currency.js'
import { isFourInput, isNumInput } from '@/utils/rules.js'
import { selectDataClient, showLoading, hideLoading } from '@/utils/common'
import { mapMutations } from 'vuex'
import historyUpdateTable from '@/pages/subcontract/components/historyUpdateTable.vue'
import fn from '@/utils/checkEmpty.js'
export default {
    props: {
        billid: {
            type: String,
            default: ''
        },
        contractListTable: { //组件绑定的数据
            type: Array,
            default: function () {
                return []
            }
        },
        errorMsg: { //表格错误提示
            type: Array,
            default: function () {
                return []
            }
        },
        isBidding: { //是否是招标
            type: Boolean,
            default: false
        },
        mainInfo: { //主体信息
            type: Object,
            default: function () {
                return {}
            }
        },
        auditState: { //是否可以编辑
            type: Object,
            default: function () {
                return {
                    canAudit: false,
                    canModify: true,
                    canUndoAudit: false,
                    freeNextUnits: [],
                    isFree: false,
                }
            }
        },
        //是否可以新增
        isAdd: {
            default: true
        },
        //是否可以编辑
        isEdit: {
            default: true
        },
        subcontract: { //接口
        },
        state: { //流程状态
            type: Number
        },
        period: { //变更期数
           	type: Number,
            default: 1
        }
    },
    model: {
        prop: 'contractListTable',
        event: 'change'
    },
    computed: {
        tenderId () {
            return this.$route.query.billid
        },
        isUnitprice () { //计算最子级清单是否是劳务单价
            return row=>{
                const fn  = row=>{//查找到最子级清单
                    if(row.listIsDetailed && !row.structureId) {
                        return row
                    }else{
                        return fn(this.mapTableData[row.parentId])
                    }
                }
                let list = fn(row)
                if(list.unitPriceType === 2) {
                    return true
                }else{
                    return false
                }
            }
        },
        isTerm () {//上级是否是项
            return row=>{
                if(row.parentId !== '0') {
                    if(this.mapTableData[row.parentId] && this.mapTableData[row.parentId].identification === '项') {
                        return true
                    }else{
                        return false
                    }
                }else{
                    return false
                }
            }
        },
        //截止本期末变更后数量
        quantityEndChange () {
            return row=>{
                // if( !row.itemIsDetailed && !row.structureId && row.changeState !== -1) {
                //     return 0
                // }
                const quantityEndChange = row.quantityEndChange
                row.quantityEndChange = (parseFloat(row.quantityAfterChange ? row.quantityAfterChange : 0) + parseFloat(row.quantityLastChange ? row.quantityLastChange : 0)).toFixed(4)
                if(quantityEndChange != row.quantityEndChange && row.quantityEndChange != (row.quantityLastChange ? row.quantityLastChange : 0)) {
                    this.onCalcAmount(row)
                }
                return row.quantityEndChange
            }
        },
        //截止本期末变更后单价
        unitPriceEndChange () {
            return row=>{
                // if( !row.itemIsDetailed && !row.structureId && row.changeState !== -1) {
                //     return 0
                // }
                const unitPriceEndChange = row.quantityEndChange
                row.unitPriceEndChange = (parseFloat(row.unitPriceAfterChange ? row.unitPriceAfterChange : 0) + parseFloat(row.unitPriceLastChange ? row.unitPriceLastChange : 0)).toFixed(2)
                if(unitPriceEndChange != row.unitPriceEndChange && row.unitPriceEndChange != (row.unitPriceLastChange ? row.unitPriceLastChange : 0) ) {
                    this.onCalcAmount(row)
                }
                return row.unitPriceEndChange
            }
        },
        //判断数据是否为空
        checkEmpty () {
            return datas=>{
                return !fn.checkEmpty(datas)
            }
        },
    },
    components: {
        historyUpdateTable,
    },
    watch: {
        contractListTable: {
            handler (newName) {
                this.tableData = newName
            },
            immediate: false,
            deep: true
        },
        tableData: {
            handler (newName) {
                this.$emit('change', newName)
            },
            immediate: true,
            deep: true
        },
    },
    data () {
        return {
            detailsOfProposedArr: [],
            tableData: [],
            mapTableData: {},
            addList: [], // 缓存新增数据
            unitList: [],
            searchKey: '',
            isSearch: false,
            materialList: [],
            showTable: true,
            historyTableData: {},
            changeType: 0,
        }
    },
    created () {
        console.log('subcontract', this.subcontract)
        this.getData()
    },
    mounted () {
        vueDom(
            // 默认0为选中项
            () => {
                $('.el-tree-node:first')?.addClass('is-current')
            },
            () => {
                return $('.el-tree-node:first')?.attr('class')?.includes('is-current')
            }
        )
        getUnit().then(res=>{
            let arr = res.map(x=>{
                let obj = {
                    value: '',
                    label: '',
                }
                obj.value = x.kvValue
                obj.label = x.kvKey
                return obj
            })
            this.unitList = arr
            console.log('unitList', this.unitList)
        })
    },
    methods: {
        ...mapMutations(
            'contract', ['setMaterialList']
        ),
        ...mapMutations(['setHistoryTableData']),
        ...mapMutations(['setSelectedInfo']),
        // 获取数据
        getData () {
            this.showTable = false
            const params = {
                parentId: '0',
                sourceId: this.billid
            }
            this.subcontract.getContractList(params).then(res=>{
                res = res.code ? [] : res
                res.forEach(item=>{
                    item.hasChildren = item.isDetailed ? false : true
                    item.changeType = 0
                    item.children = []
                    if(this.state === 1) {
                        item.quantityUpdateFlag = false
                        item.unitPriceUpdateFlag = false
                        item.amountUpdateFlag = false
                    }
                    if(this.period <= 1) {
                        item.quantityLastChange = item.quantity
                        item.unitPriceLastChange = item.unitPrice
                        item.amountLastChange = item.amount
                    }
                    if(item.changeState === 0 || item.changeState === null) {
                        if(this.period <= 1 ) {
                            item.quantityEndChange = item.quantity
                            item.unitPriceEndChange = item.unitPrice
                            item.amountEndChange = item.amount
                        }else{
                            item.quantityEndChange = item.quantityLastChange ? item.quantityLastChange : 0
                            item.unitPriceEndChange = item.unitPriceLastChange ? item.unitPriceLastChange : 0
                            item.amountEndChange = item.amountLastChange ? item.amountLastChange : 0
                        }
                    }
                })
                this.mapTableData['0'] = {
                    children: res
                }
                this.tableData = res
                this.showTable = true
            })
        },
        // 通过id获取子级数据
        async getById (billId) {
            const params = {
                parentId: billId,
                sourceId: this.billid
            }
            let result = await this.subcontract.getContractList(params)
            result.forEach(item=>{
                item.hasChildren = item.isDetailed ? false : true
                item.children = []
                item.changeType = 0
                item.listIsDetailed = item.listIsDetailed !== null ? item.listIsDetailed : false
                item.itemIsDetailed = item.itemIsDetailed !== null ? item.itemIsDetailed : false
                if(this.state === 1) {
                    item.quantityUpdateFlag = false
                    item.unitPriceUpdateFlag = false
                    item.amountUpdateFlag = false
                }
                if(this.period <= 1) {
                    item.quantityLastChange = item.quantity
                    item.unitPriceLastChange = item.unitPrice
                    item.amountLastChange = item.amount
                }
                if(item.changeState === 0 || item.changeState === null) {
                    if(this.period <= 1 ) {
                        item.quantityEndChange = item.quantity
                        item.unitPriceEndChange = item.unitPrice
                        item.amountEndChange = item.amount
                    }else{
                        item.quantityEndChange = item.quantityLastChange ? item.quantityLastChange : 0
                        item.unitPriceEndChange = item.unitPriceLastChange ? item.unitPriceLastChange : 0
                        item.amountEndChange = item.amountLastChange ? item.amountLastChange : 0
                    }
                }
            })
            return result
        },
        importList () {
            if(this.isSearch) return
            this.showClient()
        },
        async showClient () {
            let list = []
            showLoading()
            let listAll = await this.subcontract.contractListAll({ sourceId: this.billid })//获取所有数据
            this.setChildren([...listAll])
            let newListAll = []
            const treeChangeList = data=>{
                data.forEach(item=>{
                    if(item.children.length > 0) {
                        treeChangeList(item.children)
                    }
                    newListAll.push(item)
                })
            }
            treeChangeList(this.tableData)//将表格数据展平
            newListAll = newListAll.filter(x=>(x.changeType === -1 || x.changeType === -2) && x.oriBillId)
            const changeChildType = data=>{
                let obj = listAll.find(x=>x.billId === data.billId)
                if(obj.changeState !== 1) {
                    obj.changeType = -2
                }else{
                    obj.changeType = -1
                }
                if(!data.isDetailed) {
                    const arr = listAll.filter(x=>x.parentId === data.billId)
                    arr.forEach(item=>{
                        changeChildType(item)
                    })
                }
            }
            newListAll.forEach(item=>{
                changeChildType(item)
            })//将子级改成已删除状态
            listAll = listAll.filter(x=>x.changeState !== -1)//过滤已删除的源单并且删掉
            let structureIdArr = listAll.filter(x=>x.structureId)
            if(listAll.length > 0) {
                listAll.forEach(item=>{
                    if(item.oriBillId && !item.structureId) {
                        let obj = {
                            'identification': item.identification,
                            'isDetailed': item.oriIsDetailed ? 1 : 0, //原清单的最明细
                            'itemName': item.itemName,
                            'itemNo': item.itemNo,
                            'quantity': item.quantity,
                            'unit': item.unit,
                            'thisLevelId': item.oriBillId,
                            'termIsDetailed': item.listIsDetailed ? 1 : 0,
                            'parentId': item.oriParentBillId,
                        }
                        let arr = structureIdArr.filter(x=>x.oriParentBillId === obj.thisLevelId)//找到当前级的结构部位集合
                        arr = arr.map(x=>{
                            let obj1 = {
                                'billId': x.structureId,
                                'structureName': x.structureName,
                                'structureNamePaths': x.structureNamePaths,
                                'structurePaths': x.structurePaths,
                                'thisLevelId': x.oriBillId,
                                'parentId': x.oriParentBillId,
                                'quantity': x.quantity,
                            }
                            return obj1
                        })
                        obj.structure = arr
                        list.push(obj)
                    }

                })
            }
            hideLoading()
            selectDataClient({
                SelectServiceToken: 'EngineerSelectorImportDetail', // 客户端对应方法名
                IsSingleMode: false, // 是否单选 true 单选 false 多选
                SelectedIDs: [], // 已选中id
                InitParamsStr: JSON.stringify(list),
                Title: '选择清单' // 标题
            }).then(async val => {
                const valFilter = val.filter(x=>x.structure.length > 0)
                const newVal = []
                val.forEach(item=>{
                    newVal.push(item)
                })
                valFilter.forEach(item=>{
                    item.structure.forEach(x=>{
                        newVal.push(x)
                    })
                })
                const length = newVal.length
                const uids = await this.uidBuilder({ num: length })
                for(let i = 0;i < newVal.length;i++) {
                    newVal[i].uid = uids[i]
                }
                let arr = []
                const fn = (data, parentId)=>{
                    let arr = val.filter(x=>x.parentId === parentId)
                    let arr1 = arr.map(x=>x.itemNo)
                    arr1 = arr1.sort()
                    arr1.forEach(item=>{
                        data.push( arr.find(x=>x.itemNo === item) )
                    })
                    if(data.length > 0) {
                        data.forEach(item=>{
                            if(item.isLeaf === false) {
                                item.children = []
                            }else{
                                item.children = []
                                fn(item.children, item.thisLevelId)
                            }

                        })
                    }
                }
                fn(arr, '0')
                this.handleContractList(arr)
            })
            // let val = [{ 'billId': null, 'budgetPrice': null, 'identification': '清单', 'isDetailed': 0, 'itemName': '路基', 'itemNo': '200章', 'materialPrice': null, 'parentId': '0', 'quantity': null, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 0, 'thisLevelId': '4d9ce3e04c2d479e1af13d70b433f115', 'unit': '', 'structure': [] }, { 'billId': null, 'budgetPrice': null, 'identification': '清单', 'isDetailed': 0, 'itemName': '清理与掘除', 'itemNo': '202-1', 'materialPrice': null, 'parentId': '4d9ce3e04c2d479e1af13d70b433f115', 'quantity': null, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 0, 'thisLevelId': '3fea8cdb4870408ec83ad3093d32a549', 'unit': '', 'structure': [{ 'billId': '1565596349022007300', 'budgetPrice': null, 'identification': null, 'isDetailed': null, 'itemName': null, 'itemNo': null, 'materialPrice': null, 'parentId': '3fea8cdb4870408ec83ad3093d32a549', 'quantity': 10.2, 'structureName': '清理与掘除', 'structureNamePaths': '路基-路基土石方工程-场地清理-清理与掘除', 'structurePaths': '[{"isLeaf": 0, "listId": "69ab6e34dafd4d72b3618232a1ad9935", "parentId": "0", "structureId": "1565596349022007297", "structureNo": "2", "structureName": "路基", "structureType": 0}, {"isLeaf": 0, "listId": "658e592f577b4f9d8181c74a2e813e83", "parentId": "69ab6e34dafd4d72b3618232a1ad9935", "structureId": "1565596349022007298", "structureNo": "2-1", "structureName": "路基土石方工程", "structureType": 0}, {"isLeaf": 0, "listId": "d9d556c4e03f41c4ace81a6c4baf4890", "parentId": "658e592f577b4f9d8181c74a2e813e83", "structureId": "1565596349022007299", "structureNo": "2-1-1", "structureName": "场地清理", "structureType": 0}, {"isLeaf": 1, "listId": "a43307870690489f92b309f5b72333e2", "parentId": "d9d556c4e03f41c4ace81a6c4baf4890", "structureId": "1565596349022007300", "structureNo": "2-1-1-1", "structureName": "清理与掘除", "structureType": 0}]', 'termIsDetailed': null, 'thisLevelId': '2781eaafc0141f44f383354989558926', 'unit': null, 'structure': null }] }, { 'billId': null, 'budgetPrice': null, 'identification': '清单', 'isDetailed': 1, 'itemName': '挖竹根', 'itemNo': '202-1-d', 'materialPrice': null, 'parentId': '3fea8cdb4870408ec83ad3093d32a549', 'quantity': 10, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 1, 'thisLevelId': '2781eaafc0141f44f383354989558926', 'unit': 'm2', 'structure': [] }, { 'billId': '1562837338999615569', 'budgetPrice': 100, 'identification': '清单', 'isDetailed': 1, 'itemName': '挖树根', 'itemNo': '202-1-c', 'materialPrice': null, 'parentId': '3fea8cdb4870408ec83ad3093d32a549', 'quantity': 1, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 1, 'thisLevelId': '2e252516437a0391f7a59ec28b738e7d', 'unit': '棵', 'structure': [{ 'billId': '1565596349022007300', 'budgetPrice': 100, 'identification': null, 'isDetailed': null, 'itemName': null, 'itemNo': null, 'materialPrice': null, 'parentId': '2e252516437a0391f7a59ec28b738e7d', 'quantity': 1, 'structureName': '清理与掘除', 'structureNamePaths': '路基-路基土石方工程-场地清理-清理与掘除', 'structurePaths': '[{"isLeaf": 0, "listId": "69ab6e34dafd4d72b3618232a1ad9935", "parentId": "0", "structureId": "1565596349022007297", "structureNo": "2", "structureName": "路基", "structureType": 0}, {"isLeaf": 0, "listId": "658e592f577b4f9d8181c74a2e813e83", "parentId": "69ab6e34dafd4d72b3618232a1ad9935", "structureId": "1565596349022007298", "structureNo": "2-1", "structureName": "路基土石方工程", "structureType": 0}, {"isLeaf": 0, "listId": "d9d556c4e03f41c4ace81a6c4baf4890", "parentId": "658e592f577b4f9d8181c74a2e813e83", "structureId": "1565596349022007299", "structureNo": "2-1-1", "structureName": "场地清理", "structureType": 0}, {"isLeaf": 1, "listId": "a43307870690489f92b309f5b72333e2", "parentId": "d9d556c4e03f41c4ace81a6c4baf4890", "structureId": "1565596349022007300", "structureNo": "2-1-1-1", "structureName": "清理与掘除", "structureType": 0}]', 'termIsDetailed': null, 'thisLevelId': '1567443298222272513', 'unit': null, 'structure': null }] }, { 'billId': '1562837338999615567', 'budgetPrice': 100, 'identification': '清单', 'isDetailed': 1, 'itemName': '施工场地清理', 'itemNo': '202-1-a', 'materialPrice': null, 'parentId': '3fea8cdb4870408ec83ad3093d32a549', 'quantity': 10.2, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 1, 'thisLevelId': 'c7c8c94051baae809d991344429f605b', 'unit': 'm2', 'structure': [{ 'billId': '1565596349022007300', 'budgetPrice': 100, 'identification': null, 'isDetailed': null, 'itemName': null, 'itemNo': null, 'materialPrice': null, 'parentId': 'c7c8c94051baae809d991344429f605b', 'quantity': 10.2, 'structureName': '清理与掘除', 'structureNamePaths': '路基-路基土石方工程-场地清理-清理与掘除', 'structurePaths': '[{"isLeaf": 0, "listId": "69ab6e34dafd4d72b3618232a1ad9935", "parentId": "0", "structureId": "1565596349022007297", "structureNo": "2", "structureName": "路基", "structureType": 0}, {"isLeaf": 0, "listId": "658e592f577b4f9d8181c74a2e813e83", "parentId": "69ab6e34dafd4d72b3618232a1ad9935", "structureId": "1565596349022007298", "structureNo": "2-1", "structureName": "路基土石方工程", "structureType": 0}, {"isLeaf": 0, "listId": "d9d556c4e03f41c4ace81a6c4baf4890", "parentId": "658e592f577b4f9d8181c74a2e813e83", "structureId": "1565596349022007299", "structureNo": "2-1-1", "structureName": "场地清理", "structureType": 0}, {"isLeaf": 1, "listId": "a43307870690489f92b309f5b72333e2", "parentId": "d9d556c4e03f41c4ace81a6c4baf4890", "structureId": "1565596349022007300", "structureNo": "2-1-1-1", "structureName": "清理与掘除", "structureType": 0}]', 'termIsDetailed': null, 'thisLevelId': '1567443298205495297', 'unit': null, 'structure': null }] }, { 'billId': '1562837338999615568', 'budgetPrice': 100, 'identification': '清单', 'isDetailed': 1, 'itemName': '砍伐树木', 'itemNo': '202-1-b', 'materialPrice': null, 'parentId': '3fea8cdb4870408ec83ad3093d32a549', 'quantity': 1, 'structureName': null, 'structureNamePaths': null, 'structurePaths': null, 'termIsDetailed': 1, 'thisLevelId': '455ad5ac7fcac00feb235246368e0d4b', 'unit': '棵', 'structure': [{ 'billId': '1565596349022007300', 'budgetPrice': 100, 'identification': null, 'isDetailed': null, 'itemName': null, 'itemNo': null, 'materialPrice': null, 'parentId': '455ad5ac7fcac00feb235246368e0d4b', 'quantity': 1, 'structureName': '清理与掘除', 'structureNamePaths': '路基-路基土石方工程-场地清理-清理与掘除', 'structurePaths': '[{"isLeaf": 0, "listId": "69ab6e34dafd4d72b3618232a1ad9935", "parentId": "0", "structureId": "1565596349022007297", "structureNo": "2", "structureName": "路基", "structureType": 0}, {"isLeaf": 0, "listId": "658e592f577b4f9d8181c74a2e813e83", "parentId": "69ab6e34dafd4d72b3618232a1ad9935", "structureId": "1565596349022007298", "structureNo": "2-1", "structureName": "路基土石方工程", "structureType": 0}, {"isLeaf": 0, "listId": "d9d556c4e03f41c4ace81a6c4baf4890", "parentId": "658e592f577b4f9d8181c74a2e813e83", "structureId": "1565596349022007299", "structureNo": "2-1-1", "structureName": "场地清理", "structureType": 0}, {"isLeaf": 1, "listId": "a43307870690489f92b309f5b72333e2", "parentId": "d9d556c4e03f41c4ace81a6c4baf4890", "structureId": "1565596349022007300", "structureNo": "2-1-1-1", "structureName": "清理与掘除", "structureType": 0}]', 'termIsDetailed': null, 'thisLevelId': '1567443298213883906', 'unit': null, 'structure': null }] }]
            // const valFilter = val.filter(x=>x.structure.length > 0)
            // const newVal = []
            // val.forEach(item=>{
            //     newVal.push(item)
            // })
            // valFilter.forEach(item=>{
            //     item.structure.forEach(x=>{
            //         newVal.push(x)
            //     })
            // })
            // const length = newVal.length
            // const uids = await this.uidBuilder({ num: length })
            // for(let i = 0;i < newVal.length;i++) {
            //     newVal[i].uid = uids[i]
            // }
            // let arr = []
            // const fn = (data, parentId)=>{
            //     let arr = val.filter(x=>x.parentId === parentId)
            //     let arr1 = arr.map(x=>x.itemNo)
            //     arr1 = arr1.sort()
            //     arr1.forEach(item=>{
            //         data.push( arr.find(x=>x.itemNo === item) )
            //     })
            //     if(data.length > 0) {
            //         data.forEach(item=>{
            //             if(item.isLeaf === false) {
            //                 item.children = []
            //             }else{
            //                 item.children = []
            //                 fn(item.children, item.thisLevelId)
            //             }

            //         })
            //     }
            // }
            // fn(arr, '0')
            // this.handleContractList(arr)
        },
        //引入目标预算清单
        async handleContractList  (arr) {
            const fn =  (newData, oldData, parentId)=>{
                newData.forEach(async x=>{
                    const resul1 = oldData.find(item=>item.oriBillId === x.thisLevelId && (x.changeState !== -1))
                    if(!resul1) {
                        let obj = {
                            'amount': '',
                            'billId': '',
                            'changeType': 1,
                            'changeState': 1,
                            'children': [],
                            'itemNo': x.itemNo,
                            'identification': x.identification,
                            'itemName': x.itemName,
                            'quantity': 0,
                            'sourceId': this.billid,
                            'structuralPosition': '',
                            'unit': x.unit,
                            'unitPrice': 0,
                            'unitPriceType': '',
                            'structureId': '',
                            'structureName': '',
                            'isDetailed': false, //是否最明细
                            'oriBillId': x.thisLevelId,
                            'oriParentBillId': x.parentId,
                            'itemIsDetailed': x.isDetailed, //细目最明细
                            'listIsDetailed': x.termIsDetailed, //清单最明细
                            'amountAfterChange': 0,
                            'amountEndChange': 0,
                            'amountLastChange': 0,
                            'amountUpdateFlag': true,
                            'parentId': '',
                            'quantityAfterChange': 0,
                            'quantityEndChange': 0,
                            'quantityLastChange': 0,
                            'quantityUpdateFlag': true,
                            'remarks': '',
                            'unitPriceAfterChange': 0,
                            'unitPriceEndChange': 0,
                            'unitPriceLastChange': 0,
                            'unitPriceUpdateFlag': true
                        }
                        obj.parentId = parentId
                        obj.billId = x.uid //本级ID
                        obj.oriIsDetailed = x.isDetailed//原清单是否最明细
                        obj.hasChildren = true
                        obj.isNew = true
                        this.mapTableData[obj.billId] = obj
                        if(!x.isDetailed ) {
                            fn(x.children, obj.children, obj.billId)
                        }else{
                            const structure = x.structure
                            for(let item of structure) {
                                let obj1 = {
                                    'amount': '',
                                    'billId': item.uid,
                                    'changeType': 1,
                                    'changeState': 1,
                                    'children': [],
                                    'itemNo': '',
                                    'identification': x.identification,
                                    'itemName': '',
                                    'quantity': 0,
                                    'sourceId': this.billid,
                                    'structuralPosition': '',
                                    'unit': x.unit,
                                    'unitPrice': 0,
                                    'unitPriceType': '',
                                    'structureId': item.billId,
                                    'structureName': item.structureName,
                                    'structureNamePaths': item.structureNamePaths,
                                    'structurePaths': item.structurePaths,
                                    'isDetailed': true, //是否最明细
                                    'oriBillId': x.thisLevelId,
                                    'oriParentBillId': x.parentId,
                                    'itemIsDetailed': false, //细目最明细
                                    'listIsDetailed': false, //清单最明细
                                    'oriIsDetailed': null,
                                    'useSupplyFlag': false, //是否参与甲供清单的计算
                                    'hasChildren': false,
                                    'amountAfterChange': 0,
                                    'amountEndChange': 0,
                                    'amountLastChange': 0,
                                    'amountUpdateFlag': true,
                                    'parentId': '',
                                    'quantityAfterChange': item.quantity ? item.quantity : 0,
                                    'quantityEndChange': 0,
                                    'quantityLastChange': 0,
                                    'quantityUpdateFlag': true,
                                    'remarks': '',
                                    'unitPriceAfterChange': 0,
                                    'unitPriceEndChange': 0,
                                    'unitPriceLastChange': 0,
                                    'unitPriceUpdateFlag': true
                                }
                                obj1.parentId = obj.billId
                                obj1.isNew = true
                                this.mapTableData[obj1.billId] = obj1
                                this.onCalcAmount(obj1)
                                obj.children.push(obj1)
                            }
                            // 统计所有同级
                            const countSibling = obj.children.reduce((prev, item) => {
                                return prev + Math.round((item.changeState !== -1) ? item.quantityAfterChange : 0)
                            }, 0)
                            // 赋值汇总数据给父级
                            obj.quantityAfterChange = countSibling // 赋值汇总数据给父级
                        }
                        oldData.push(obj)
                    }else{
                        if(!x.isDetailed ) {
                            if(resul1.children.length === 0) {
                                resul1.billId = resul1.thisLevelId ? resul1.thisLevelId : resul1.billId
                                const res = await this.getById(resul1.billId, resul1)
                                res.forEach(item=>{
                                    resul1.children.push(item)
                                })
                            }
                            fn(x.children, resul1.children, resul1.billId)
                        }
                    }

                })
            }

            fn(arr, this.tableData, '0')
        },
        //计算物资清单
        getmMaterialList (row, operation = false) {
            if(!row.structureId) return
            let itemNo = ''
            const fn = item=>{
                if(item.identification !== '清单') {
                    const row = this.mapTableData[item.parentId]
                    fn(row)
                }else{
                    itemNo = item.copyItemNo
                }
            }
            fn(row)
            const material = {
                billId: row.billId,
                structureId: row.structureId,
                structureName: row.structureName,
                changeType: row.changeType,
                operation: operation,
                itemNo: itemNo
            }
            setTimeout(()=>{ //转为异步，否则连续多次执行不会触发监听
                this.setMaterialList(material)
            }, 10)
        },
        //设置树的数据
        setChildren (newData) {
            const fn = data=>{
                //找到同级的新数据
                if(!data.isDetailed) {
                    const newArr = newData.filter(x=>x.parentId === data.billId)
                    data.children =  data.children ? data.children : []
                    newArr.forEach(item=>{
                        const obj = this.mapTableData[item.billId]
                        if(!obj) {
                            item.hasChildren = item.isDetailed ? false : true
                            item.changeType = 0
                            item.children = []
                            if(this.state === 1) {
                                item.quantityUpdateFlag = false
                                item.unitPriceUpdateFlag = false
                                item.amountUpdateFlag = false
                            }
                            if(this.period <= 1) {
                                item.quantityLastChange = item.quantity
                                item.unitPriceLastChange = item.unitPrice
                                item.amountLastChange = item.amount
                            }
                            if(item.changeState === 0 || item.changeState === null) {
                                if(this.period <= 1 ) {
                                    item.quantityEndChange = item.quantity
                                    item.unitPriceEndChange = item.unitPrice
                                    item.amountEndChange = item.amount
                                }else{
                                    item.quantityEndChange = item.quantityLastChange ? item.quantityLastChange : 0
                                    item.unitPriceEndChange = item.unitPriceLastChange ? item.unitPriceLastChange : 0
                                    item.amountEndChange = item.amountLastChange ? item.amountLastChange : 0
                                }
                            }
                            data.children.push(item)
                        }
                    })
                    data.children.forEach(item=>{
                        fn(item)
                    })
                    this.mapTableData[data.billId] = data
                }
            }
            this.tableData.forEach(item=>{
                fn(item)
            })
        },
        //手动计算甲供清单
        supplyCount (row, operation) {
            // let structureId = row.structureId
            const fn  = row=>{//查找到最子级清单
                if(row.listIsDetailed) {
                    return row
                }else{
                    return fn(this.mapTableData[row.parentId])
                }
            }
            let list = fn(row)
            // const fn1 = async row=>{//给下面所有同结构部位改成已使用状态
            //     if(!row.isDetailed) {
            //         if(row.children.length > 0) {
            //             row.children.forEach(item=>{
            //                 fn1(item)
            //             })
            //         }else{
            //             let result = await this.getById(row.billId)
            //             result.forEach(item=>{
            //                 if(row.unit !== '总额' && row.itemIsDetailed) {
            //                     if(item.unitPrice !== row.unitPrice) {
            //                         item.unitPrice = row.unitPrice
            //                         this.onCalcAmount(item)
            //                     }
            //                 }
            //             })
            //             row.children = result
            //             row.children.forEach(item=>{
            //                 this.mapTableData[item.billId] = item
            //                 this.onCalcAmount(item)
            //                 fn1(item)
            //             })
            //         }
            //     }else{
            //         if(row.structureId === structureId) {
            //             row.useSupplyFlag = true
            //         }
            //     }
            //     this.mapTableData[row.billId] = row
            // }
            // fn1(list)
            row.copyItemNo = list.itemNo
            this.getmMaterialList(row, operation)
        },
        async onload (row, treeNode, resolve) {
            let result
            if(row.children.length > 0) {
                result = row.children
            }else{
                // 如果不是最子级，就从接口取
                if (!row.isDetailed) {
                    row.billId = row.thisLevelId ? row.thisLevelId : row.billId
                    result = await this.getById(row.billId, row)
                } else {
                // 是最子级，则初始为空，合并新增数据，并设为非最子级
                    result = []
                    row.isDetailed = false
                }
                // 如果有缓存数据，说明是点击的 “添加子级” 按钮
                if (this.addList.length > 0) {
                    result = result.concat(this.addList)
                    this.addList = []
                }
            }
            row.treeNode = treeNode
            row.resolve = resolve
            row.children = result
            result.forEach(item=>{
                if(row.unit !== '总额' && row.itemIsDetailed) {
                    if(item.unitPrice !== row.unitPrice) {
                        item.unitPrice = row.unitPrice
                        item.oriPrice = row.oriPrice
                        this.onCalcAmount(item)
                    }
                }
                if(row.changeState === -1 ) {
                    item.changeState === row.changeState
                    item.changeType === row.changeType
                }
                this.mapTableData[item.billId] = item
            })
            this.mapTableData[row.billId] = row
            resolve(result)
        },
        // 添加子级
        async addChilden (row) {
            const { billId } = row
            const uid = await this.uidBuilder({ num: 1 })
            let addData = {
                'amountUpdateFlag': false,
                'changeState': 1,
                'itemIsDetailed': true,
                'oriParentBillId': '',
                'quantityUpdateFlag': false,
                'remarks': '',
                'structureNamePaths': '',
                'structurePaths': '',
                'unitPriceUpdateFlag': false,
                'amount': '',
                'changeType': 1,
                'children': [],
                'itemNo': '',
                'identification': '项',
                'itemName': '',
                'quantity': 0,
                'sourceId': this.billid,
                'structuralPosition': '',
                'unit': '',
                'unitPrice': 0,
                'unitPriceType': row.unitPriceType,
                'structureId': '',
                'structureName': '',
                'oriIsDetailed': null,
                'oriBillId': '',
                listIsDetailed: false,
                parentId: billId, //父级ID
                billId: uid, //本级ID
                isDetailed: true,
                hasChildren: false,
                isNew: true,
                'amountAfterChange': 0,
                'amountEndChange': 0,
                'amountLastChange': 0,
                'quantityAfterChange': '',
                'quantityEndChange': 0,
                'quantityLastChange': 0,
                'unitPriceAfterChange': 0,
                'unitPriceEndChange': 0,
                'unitPriceLastChange': 0,
            }
            addData.itemIsDetailed = true//是否是明细最子级
            /*
                如果是清单最子级新增，下面没有项则新增一项，结构部位下移到项，如果有项，则新增一项，将其他最子级项的结构部位复制到新增项里面
                如果是项新增子级，则在改项下新增一级项，把结构部位下移到新增项下面
            */
            const getChildren = ()=>{//获取结构部位层
                return new Promise(resolve=>{
                    let children = []
                    const fn  = async data=>{
                        data = data.filter(x=>x.changeState !== -1)
                        if(data[0].isDetailed) {
                            const index = data.length
                            data.forEach(async (x, i)=>{
                                if(x.changeState !== -1) {
                                    let obj = {
                                        'amount': '',
                                        'changeType': x.changeType,
                                        'children': [],
                                        'itemNo': '',
                                        'identification': '项',
                                        'itemName': '',
                                        'quantity': 0,
                                        'sourceId': this.billid,
                                        'structuralPosition': '',
                                        'unitPrice': 0,
                                        'unitPriceType': '',
                                        'structureId': x.structureId,
                                        'structureName': x.structureName,
                                        'oriIsDetailed': null,
                                        'oriBillId': x.oriBillId,
                                        listIsDetailed: false,
                                        parentId: '', //父级ID
                                        billId: x.billId, //本级ID
                                        isDetailed: true,
                                        hasChildren: false,
                                        isNew: true,
                                        //后续新增的字段
                                        'amountUpdateFlag': false,
                                        'changeState': 1,
                                        'itemIsDetailed': null,
                                        'oriParentBillId': x.oriParentBillId,
                                        'quantityUpdateFlag': false,
                                        'remarks': '',
                                        'structureNamePaths': x.structureNamePaths,
                                        'structurePaths': x.structurePaths,
                                        'unit': '',
                                        'unitPriceUpdateFlag': false,
                                        'amountAfterChange': 0,
                                        'amountEndChange': 0,
                                        'amountLastChange': 0,
                                        'quantityAfterChange': '',
                                        'quantityEndChange': 0,
                                        'quantityLastChange': 0,
                                        'unitPriceAfterChange': 0,
                                        'unitPriceEndChange': 0,
                                        'unitPriceLastChange': 0,
                                    }
                                    addData.isDetailed = false
                                    addData.hasChildren = true
                                    children.push(obj)
                                }
                                if(i === index - 1) {
                                    resolve(children)
                                }
                            })
                        }else{
                            if(data[0].children.length > 0) {
                                fn(data[0].children)
                            }else{
                                const params = {
                                    parentId: data[0].billId,
                                    sourceId: this.billid
                                }
                                let result = await this.subcontract.getContractList(params)
                                fn(result)
                            }
                        }
                    }
                    if(!row.isDetailed) { //如果不是最明细就往下找
                        if(row.children.length > 0) {
                            fn(row.children)
                        }else{
                            const params = {
                                parentId: row.billId,
                                sourceId: this.billid
                            }
                            this.subcontract.getContractList(params).then(result=>{
                                fn(result)
                            })
                        }
                    }
                })
            }
            getChildren().then(children=>{
                new Promise(resolve=>{
                    let parentList = null //储存有其他分支的父级
                    const fn = data=>{ //向上查找是否有其他分支
                        const parent = this.mapTableData[data.parentId]
                        const noDeletChildren = parent.children.filter(x=>x.changeState !== -1)
                        if(data.changeState !== -1) {
                            data.changeType = data.changeType === 1 ? 1 : 2
                            data.changeState = data.changeType === 1 ? 1 : 2
                        }
                        if(noDeletChildren.length > 1 || data.parentId == '0') {
                            parentList = data
                        }else{
                            fn(parent)
                        }
                    }
                    fn(row)
                    if(row.itemIsDetailed ) {
                        row.children.splice(0)
                        if(parentList.changeState !== 1) {
                            children.forEach(item=>{
                                item.parentId = addData.billId
                                if(item.changeState !== -1) {
                                    item.changeType = 2
                                    item.changeState = 2
                                }
                            })
                        }
                        resolve(children)
                    }else{
                        this.uidBuilder({ num: children.length }).then(uids=>{
                            uids = children.length > 1 ? uids : [uids]
                            children.forEach((item, index)=>{
                                item.parentId = addData.billId
                                item.billId = uids[index]
                                item.changeType = 1
                                item.changeState = 1
                            })
                            resolve(children)
                        })
                    }
                }).then(children=>{
                    addData.children = children ? children : []
                    this.mapTableData[addData.billId] = addData
                    const tableRef = this.$refs.tableRef
                    if (!this.mapTableData[billId]) {
                        // 缓存新增数据
                        this.addList.push(addData)
                        // 如果是最子级，则设为目录
                        if (!row.hasChildren) {
                            row.children = row.children ? row.children : []
                            row.children = row.children.length > 0 ? row.children.push(addData) : [addData]
                            row.hasChildren = true
                            row.isDetailed = false
                            this.mapTableData[billId] = row
                            this.onCalcAmount(addData)
                            // 切换展开状态
                            this.$nextTick(() => {
                                // tableRef.toggleRowExpansion(row, true)
                                tableRef.store.loadOrToggle(row)
                            })
                        // }
                        } else {
                            // 展开子级
                            // tableRef.store.loadData(row, billId)
                            tableRef.store.loadOrToggle(row)
                        }
                    } else {
                        // 已经展开，则直接添加
                        row.children.push(addData)
                        row.hasChildren = true
                        row.isDetailed = false
                    }
                    row.itemIsDetailed = false

                    // 定位到新添加行
                    toLocation(addData)
                })

            })
        },
        //删除节点
        onDelNode (row) {
            if(row.changeState === 0 || row.changeState === 2 && !row.structureId) {
                this.deleteOldChild(row)
            }else if(row.changeState === 1 && !row.structureId) {
                this.deleteNewChild(row)
            }else{
                this.deleteStructure(row)
            }
        },
        //删除非新增的数据
        deleteOldChild (row) {
            const structure = []
            const fn = async data=>{//向下删除
                if(data.children.length > 0) {
                    for(let i = 0;i < data.children.length;i++) {
                        if(data.children[i].changeType === 1) {//如果是新增为保存的数据
                            data.children.splice(i, 1)
                            i--
                        }else{
                            if( data.children[i].changeState === 1 && data.children[i].changeType !== 1) {
                                //新增已保存的数据
                                data.children[i].changeType = -1
                                data.children[i].changeState = -1
                            }else{
                                //非新增数据
                                data.children[i].changeType = -2
                                data.children[i].changeState = -1
                            }
                            if(data.children[i].structureId && data.children[i].changeState !== 1) {
                                if(structure.find(x=>x.oriBillId === data.children[i].oriBillId)) {
                                    structure.push(data.children[i])
                                }
                            }
                            fn(data.children[i])
                        }
                    }
                }else{
                    if(!data.isDetailed) {
                        data.billId = data.thisLevelId ? data.thisLevelId : data.billId
                        const result = await this.getById(data.billId, data)
                        data.children = result
                        fn(data)
                    }
                }
            }
            fn(row)
            if(row.changeType === 1) {
                const parent = this.mapTableData[row.parentId]
                const index = parent.children.findIndex(x=>x.billId === row.billId)
                parent.children.splice(index, 1)
            }
            this.deleteParent(row, structure)
        },
        //删除新增的数据
        deleteNewChild (row) {
            const structure = []
            const fn = async data=>{//向下删除
                if(data.children.length > 0) {
                    for(let i = 0;i < data.children.length;i++) {
                        if(data.children[i].changeType === 1) {//如果是新增为保存的数据
                            data.children.splice(i, 1)
                            i--
                        }else{//如果不是则肯定是新增已保存的数据，直接删除再向下查找
                            data.children[i].changeType = -1
                            data.children[i].changeState = -1
                            if(data.children[i].structureId && data.children[i].changeState !== 1) {
                                structure.push(data.children[i])
                            }
                            fn(data.children[i])
                        }
                    }
                }else{
                    if(!data.isDetailed) {
                        data.billId = data.thisLevelId ? data.thisLevelId : data.billId
                        const result = await this.getById(data.billId, data)
                        data.children = result
                        fn(data)
                    }
                }
            }
            fn(row)
            // if(row.changeType === 1) {
            //     const parent = this.mapTableData[row.parentId]
            //     const index = parent.children.findIndex(x=>x.billId === row.billId)
            //     parent.children.splice(index, 1)
            // }
            this.deleteParent(row, structure)
        },
        //删除结构部位
        deleteStructure (row) {
            const parent = this.mapTableData[row.parentId]
            //找到父级未删除的数据
            const noDeletChildren = parent.children.filter(x=>x.changeState !== -1)
            if(noDeletChildren.length > 1) {
                //如果有两条以上的数据则只删除当前数据，并将父级状态改为编辑
                if(row.changeType === 1) {
                    const index = parent.children.findIndex(x=>x.billId === row.billId)
                    parent.children.splice(index, 1)
                }else{
                    if(row.changeState === 1) {
                        row.changeState = -1
                        row.changeType = -1
                    }else{
                        row.changeState = -1
                        row.changeType = -2
                    }
                }
            }else{
                //如果只有一条，则要同时删除父级
                if(parent.changeState === 1) {
                    this.deleteNewChild(parent)
                }else{
                    this.deleteOldChild(parent)
                }
            }

        },
        //删除本级及父级
        deleteParent (row, structure) {
            let parentList = null //储存有其他分支的父级
            const fn = data=>{ //向上查找是否有其他分支
                const parent = this.mapTableData[data.parentId]
                const noDeletChildren = parent.children.filter(x=>x.changeState !== -1)
                if(noDeletChildren.length > 1 || data.parentId == '0') {
                    parentList = data
                }else{
                    fn(parent)
                }
            }
            fn(row)
            //向下删除至当前节点
            const deleteNode = data=>{

                const parent = this.mapTableData[data.parentId]
                //如果是新增的数据则找到父级，然后将结构部位添加到父级并改成删除状态
                if(data.changeType === 1) {
                    const index = parent.children.findIndex(x=>x.billId === data.billId)
                    // let structureList = []
                    // const getStru = data=>{ //获取结构部位
                    //     data.forEach(item=>{
                    //         if(item.structure && (item.changeState === 0 || item.changeState === 2)) {
                    //             structureList.push(data)
                    //         }else if(!item.structure) {
                    //             getStru(item)
                    //         }
                    //     })
                    // }
                    // getStru(parent.children[index])
                    if(index !== -1) {
                        parent.children.splice(index, 1)
                        structure.forEach(item=>{
                            item.changeState = -1
                            item.changeType = -2
                            item.parentId = parent.billId
                            parent.children.push(item)
                        })
                    }
                }else{
                    if(data.changeState === 1) {
                        data.changeState = -1
                        data.changeType = -1
                        structure.forEach(item=>{
                            item.parentId = parent.billId
                            item.changeState = -1
                            item.changeType = -2
                        })
                    }else{
                        data.changeState = -1
                        data.changeType = -2
                        if(data.billId !== row.billId) {
                            deleteNode(data.children[0])
                        }
                    }
                }
            }
            deleteNode(parentList)
            parentList.amount = 0
            this.onCalcAmount(parentList)
        },
        //改变祖先状态
        parentChangeType (row) {
            if(row.parentId !== '0') {
                row.changeType = row.changeType === 1 ? 1 : 2
                row.changeState = row.changeState === 1 ? 1 : 2
                const parent = this.mapTableData[row.parentId]
                this.parentChangeType(parent)
            }else{
                row.changeType = row.changeType === 1 ? 1 : 2
                row.changeState = row.changeState === 1 ? 1 : 2
            }
        },
        //取消删除
        cancelDelete (row) {
            const fn = data=>{//恢复本级及下级
                if(data.changeState !== 1) {
                    data.changeState = 2
                    data.changeType = 2
                }
                if(data.structureId) {
                    this.onCalcAmount(data)
                }
                if(data.children.length > 0) {
                    data.children.forEach(item=>{
                        fn(item)
                    })
                }
            }
            fn(row)
            const fn1 = data=>{//恢复上级
                let treeId = data.parentId
                while(treeId || treeId !== '0') {
                    const parent = this.mapTableData[treeId]
                    if(!parent) return
                    parent.changeState = 2
                    parent.changeType = 2
                    if(parent.parentId === treeId) return
                    treeId = parent.parentId
                }
            }
            fn1(row)
        },
        // 输入后计划金额
        onCalcAmount (row, feild) {
            this.$emit('isMonitorTaxRate')
            if(this.state === 1 && feild) {
                row[feild] = true
            }
            if(!row.structureId) return
            if(row.structureId && this.showParentUnit(row) === '总额') {
                console.log('')
            }else{
                if(row.changeState === 1) {
                    row.amountAfterChange = row.changeState !== -1 ? Math.round(( parseFloat(row.quantityAfterChange) ? parseFloat(row.quantityAfterChange) : 0 ) * ( parseFloat(row.unitPriceAfterChange) ? parseFloat(row.unitPriceAfterChange) : 0 ))  : 0
                }else{
                    row.amountAfterChange = row.changeState !== -1 ? Math.round(( parseFloat(row.quantityAfterChange) ? parseFloat(row.quantityAfterChange) : 0 ) * ( parseFloat(row.unitPriceLastChange) ? parseFloat(row.unitPriceLastChange) : 0 ))  : 0
                }

                row.amountEndChange = row.changeState !== -1 ? Math.round(( parseFloat(row.quantityEndChange) ? parseFloat(row.quantityEndChange) : 0 ) * ( parseFloat(row.unitPriceEndChange) ? parseFloat(row.unitPriceEndChange) : 0 ))  : 0
                row.amountAfterChange = row.amountAfterChange ? row.amountAfterChange : 0
                row.amountEndChange = row.amountEndChange ? row.amountEndChange : 0
            }

            if((row.changeState !== -1)) {
                row.changeType = row.changeType === 1 ? 1 : 2
                row.changeState = row.changeState === 1 ? 1 : 2
            }
            // 向上汇总金额
            this.recurCount({
                parentId: row.parentId,
                rootId: '0',
            })
        },
        // 递归统计父级
        // recurCount ({ parentId, rootId, listField, parentField, row }) {
        //     // 到根节点
        //     if (parentId === rootId) {
        //         return
        //     }
        //     const parent = this.mapTableData[parentId]
        //     // 统计所有同级
        //     let countSibling = parent.children.reduce((prev, item) => {
        //         item.amountEndChange = item.amountEndChange ? parseInt(item.amountEndChange) : item.amountEndChange
        //         return prev + ((item.changeState !== -1) ? item.amountEndChange : 0)
        //     }, 0)
        //     // 赋值汇总数据给父级
        //     countSibling = typeof (countSibling) === 'string' ? parseInt(countSibling) : countSibling
        //     parent.amountEndChange = parseInt(countSibling.toFixed(2))
        //     // 统计所有同级
        //     let countSibling1 = parent.children.reduce((prev, item) => {
        //         item.amountAfterChange = item.amountAfterChange ? parseInt(item.amountAfterChange) : item.amountAfterChange
        //         return prev + ((item.changeState !== -1) ? item.amountAfterChange : 0)
        //     }, 0)
        //     countSibling1 = typeof countSibling === 'string' ? parseInt(countSibling1) : countSibling1
        //     parent.amountAfterChange = parseInt(countSibling1)
        //     if(parent.changeType !== -1 && parent.changeType !== -2) {
        //         parent.changeType = parent.changeType === 1 ? 1 : 2
        //         parent.changeState = parent.changeState === 1 ? 1 : 2
        //     }
        //     // 未到根节点则继续遍历父节点
        //     return this.recurCount({
        //         parentId: parent[parentField],
        //         rootId,
        //         listField,
        //         parentField,
        //         row,
        //     })
        // },
        // 循环+统计父级
        recurCount ({ parentId }) {
            let treeId = parentId
            while(treeId || treeId !== '0') {
                const parent = this.mapTableData[treeId]
                if(!parent) return
                // 统计所有同级
                let countSibling = parent.children.reduce((prev, item) => {
                    item.amountEndChange = item.amountEndChange ? parseInt(item.amountEndChange) : item.amountEndChange
                    return prev + ((item.changeState !== -1) ? item.amountEndChange : 0)
                }, 0)
                // 赋值汇总数据给父级
                countSibling = typeof countSibling === 'string' ? parseInt(countSibling) : countSibling
                parent.amountEndChange = parseInt(countSibling)
                // 统计所有同级
                let countSibling1 = parent.children.reduce((prev, item) => {
                    item.amountAfterChange = item.amountAfterChange ? parseInt(item.amountAfterChange) : item.amountAfterChange
                    return prev + ((item.changeState !== -1) ? item.amountAfterChange : 0)
                }, 0)
                countSibling1 = typeof countSibling === 'string' ? parseInt(countSibling1) : countSibling1
                parent.amountAfterChange = parseInt(countSibling1)
                if(parent.changeType !== -1 && parent.changeType !== -2) {
                    parent.changeType = parent.changeType === 1 ? 1 : 2
                    parent.changeState = parent.changeState === 1 ? 1 : 2
                }
                if(parent.parentId === treeId) return
                treeId = parent.parentId
            }

        },
        tableRowClassName ({ row }) {
            if(row.changeType === -1) {
                return `class-${row.billId} row-hide`
            }else{
                if(row.changeState === 1) {
                    return `class-${row.billId} green row-show`
                }else if(row.changeState === 2) {
                    return `class-${row.billId} yellow row-show`
                }else if(row.changeState === -1) {
                    return `class-${row.billId} red row-show`
                }else{
                    return `class-${row.billId} row-show`
                }
            }
        },

        async uidBuilder (params) {
            let temp = ''
            await uidBuilder(params).then(res => {
                temp = res.length === 1 ? res[0] : res
            })
            return temp
        },
        // refreshLoadTree (parentId) {
        //     // 根据父级id取出对应节点数据
        //     // const { row, treeNode, resolve } = this.mapTableData[parentId]
        //     let children = this.mapTableData[parentId].children
        //     const tableRef = this.$refs.tableRef
        //     this.$set(tableRef.store.states.lazyTreeNodeMap, parentId, children)
        // },

        isFourInput (event) {
            isFourInput(event)
        },
        isNumInput (event) {
            isNumInput(event)
        },
        search () {
            this.isSearch = false
            this.searchData = []
            this.$emit('isSearch', this.isSearch)
            if(this.searchKey.length > 0) {
                this.searchData = []
                this.subcontract.searchContractList({
                    key: this.searchKey,
                    sourceId: this.billid
                }).then(res=>{
                    this.isSearch = true
                    //平级列表转成树形结构
                    let arr = []
                    const fn = (data, parentId)=>{
                        let arr = res.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.itemNo)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.itemNo === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                item.hasChildren = !item.isDetailed
                                item.changeType = 0
                                if(item.isDetailed) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.billId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.searchData = arr
                    this.$emit('isSearch', this.isSearch, this.searchData, this.searchKey)
                })
            }
        },
        //单价类型切换
        unitPriceTypeChange (row) {
            const fn = async data=>{
                if(!data.isDetailed) {
                    if(data.children.length > 0) {
                        data.children.forEach(item=>{
                            item.unitPriceType = data.unitPriceType
                            this.mapTableData[data.billId] = data
                            fn(item)
                        })
                    }else{
                        const params = {
                            parentId: data.billId,
                            sourceId: this.billid
                        }
                        let result = await this.subcontract.getContractList(params)
                        data.children = result.map(x=>{
                            x.changeType = 0
                            x.hasChildren = !x.isDetailed
                            x.children = []
                            return x
                        })
                        fn(data)
                    }
                }else{
                    this.editChangeType(data)
                    if(data.identification === '清单') {
                        if(this.isUnitprice(data)) {
                            this.supplyCount(data)
                        }else{
                            this.supplyCount(data, true)
                        }
                    }
                }

            }
            if(!row.isDetailed) {
                fn(row)
            }else{
                this.mapTableData[row.billId] = row
            }
        },
        //改变当前分支的changeType
        editChangeType (row) {
            const fn = row=>{
                row.changeType = row.changeType === 1 ? 1 : 2
                row.changeState = row.changeState === 1 ? 1 : 2
                if(row.parentId !== '0') {
                    const parent = this.mapTableData[row.parentId]
                    fn(parent)
                }
            }
            fn(row)
        },
        //单价切换
        unitPriceChange (row) {
            if(this.state === 1) {
                row.unitPriceUpdateFlag = true
            }
            if(row.children.length > 0) {
                row.children.forEach(item=>{
                    item.unitPriceAfterChange = row.unitPriceAfterChange
                    this.mapTableData[item.billId] = item
                    this.onCalcAmount(item)
                })
                this.mapTableData[row.billId] = row
            }else{
                this.onCalcAmount(row)
            }
        },
        //数量切换
        quantityChange (row) {
            if(this.state === 1) {
                row.quantityUpdateFlag = true
            }
            const parent = this.mapTableData[row.parentId]
            // 统计所有同级
            const countSibling = parent.children.reduce((prev, item) => {
                let quantity = parseInt((item.changeState !== -1) ? item.quantityAfterChange : 0)
                quantity = quantity ? quantity : 0
                return prev + quantity
            }, 0)
            // 赋值汇总数据给父级
            parent.quantityAfterChange = countSibling // 赋值汇总数据给父级
            this.onCalcAmount(row)
        },
        //计量单位切换
        changeUnit (row) {
            if(row.unit === '总额') {
                row.quantityAfterChange = ''
                row.unitPriceAfterChange = ''
                row.children.forEach(item=>{
                    item.quantityAfterChange = ''
                    item.unitPriceAfterChange = ''
                    item.amountAfterChange = ''
                    this.onCalcAmount(item)
                })
            }

        },
        //金额切换
        changeAmount (row) {
            if(this.state === 1) {
                row.amountUpdateFlag = true
            }
            row.amountEndChange =  parseFloat(row.amountLastChange ? row.amountLastChange : 0)  + parseFloat(row.amountAfterChange ? row.amountAfterChange : 0)
            this.onCalcAmount(row)
        },
        clearData () {
            const arr = [ ...this.tableData]
            const deleteFn = row=>{
                const deleteStru = row=>{//删除结构部位
                    const siblings = this.mapTableData[row.parentId].children
                    const index = siblings.findIndex(x=>x.billId === row.billId)
                    if(siblings[index].changeType === 1 && siblings[index].changeState === 1) {
                        this.getmMaterialList({ ...siblings[index] }, true)
                        siblings.splice(index, 1)
                    }else if(siblings[index].changeType !== 1 && siblings[index].changeState === 1) {
                        siblings[index].changeType = -1
                        siblings[index].changeState = -1
                        this.getmMaterialList({ ...siblings[index] })
                    }
                    else {
                        siblings[index].changeType = -2
                        siblings[index].changeState = -1
                        this.getmMaterialList({ ...siblings[index] })
                    }
                    this.onCalcAmount(row)
                }

                const fn1 = data=>{ //向下删除
                    if(data.children.length > 0) {
                        for(let i = 0;i < data.children.length;i++)
                        {
                            fn1(data.children[i])
                            if(data.children[i].changeType === 1 && data.children[i].changeState === 1) {
                                data.children.splice(i, 1)
                                i--
                            }else if(data.children[i].changeType !== 1 && data.children[i].changeState === 1) {
                                data.children[i].changeType = -1
                                data.children[i].changeState = -1
                            }
                            else{
                                data.children[i].changeType = -2
                                data.children[i].changeState = -1
                            }
                            this.onCalcAmount(data.children[i])
                        }
                    }else{
                        deleteStru(data)
                    }
                }
                fn1(row)
                const fn = data=>{ //向上删除
                    if(data.parentId !== '0') {
                        const siblings = this.mapTableData[data.parentId].children
                        const index = siblings.findIndex(x=>x.billId === data.billId)
                        const filterSibings = siblings.filter(x=>(x.changeType !== -1 && x.changeState !== -2))
                        if(filterSibings.length > 1) {
                            if(siblings[index].changeType === 1 && siblings[index].changeState === 1) {
                                siblings.splice(index, 1)
                            }else if(siblings[index].changeType !== 1 && siblings[index].changeState === 1) {
                                siblings[index].changeType = -1
                                siblings[index].changeState = -1
                            }
                            else{
                                siblings[index].changeType = -2
                                siblings[index].changeState = -1
                            }
                            this.onCalcAmount(siblings[index])
                        }else{
                            fn(this.mapTableData[data.parentId])
                        }
                    }else{
                        const index = this.tableData.findIndex(x=>x.billId === data.billId)

                        if(this.tableData[index].changeType === 1 && this.tableData[index].changeState === 1) {
                            this.tableData.splice(index, 1)
                        }else if(this.tableData[index].changeType !== 1 && this.tableData[index].changeState === 1) {
                            this.tableData[index].changeType = -1
                            this.tableData[index].changeState = -1
                        }
                        else{
                            this.tableData[index].changeType = -2
                            this.tableData[index].changeState = -1
                        }
                        this.mapTableData['0'].children = this.tableData
                    }
                }
                fn(row)
            }
            arr.forEach(item=>{
                deleteFn(item)
            })
        },
        clickIcon (list) {
            this.setHistoryTableData(list)
        },
        showParentUnit (row) {
            try{
                const parent = this.mapTableData[row.billId].unit
                return parent
            }catch(err) {
                return ''
            }
        }

    }
}
</script>
<style lang='scss' scoped>
    ::v-deep .hide-icon{
        .el-table__expand-icon{
            display: none;
        }
    }
    .right{
        .el-input {
            width:350px
        }
    }
    ::v-deep.el-table__row{
        .cell{
            display: flex;
            align-items: center;
        }
        td:nth-child(5),td:nth-child(6),td:nth-child(7){
            .cell{
                display: block;
            }
        }
        td:nth-child(8),td:nth-child(9),td:nth-child(10){
            .cell{
                justify-content: flex-end;
            }
        }
    }
    ::v-deep .el-popover__reference-wrapper{
        display: flex;
        height: 100%;
    }
</style>
